package presentation;

import java.awt.Container;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.sql.Date;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import controle.ControlJButtonSaveNote;

import abstraction.Cours;
import abstraction.Etudiant;
import abstraction.Note;

/**classe qui crée la fenetre qui s'affiche lors de la création ou de la modification d'un module
 * 
 * @author PICOSSON
 *
 */
public class NoteDialog extends JDialog {


	private static final long serialVersionUID = 1L;
	private JTextField note;
	private JTextField correcteur;
	private JTextField date_correction;
	private JTextField email;
	private JCheckBox definitive;
	private Cours modele;
	private boolean creation;
/**
 * 
 * @param parent
 * @param modele
 */
	public NoteDialog(Frame parent, Cours modele) {
		super(parent, "Eleve");
		this.modele = modele;
		Container principal = this.getContentPane();
		principal.setLayout(new GridLayout(1, 2));

		JPanel panel_fixe = new JPanel(new GridLayout(7, 1));
		panel_fixe.add(new JLabel("Nom"));
		JTextField nom = new JTextField();
		panel_fixe.add(nom);
		panel_fixe.add(new JLabel("Prenom"));
		JTextField prenom = new JTextField();
		panel_fixe.add(prenom);
		panel_fixe.add(new JLabel("E-mail"));
		JTextField email = new JTextField();
		panel_fixe.add(email);
		JPanel boutons = new JPanel();
		JButton save = new JButton("OK");
		save.setMnemonic(KeyEvent.VK_ENTER);
		boutons.add(save);
		panel_fixe.add(boutons);
		this.getRootPane().setDefaultButton(save);

		principal.add(panel_fixe);

		Etudiant etudiant_courant = modele.findEtudiant(modele
				.getEtudiantCourant());
		nom.setText(etudiant_courant.getNom());
		prenom.setText(etudiant_courant.getPrenom());
		email.setText(etudiant_courant.getEmail());

		nom.setEnabled(false);
		prenom.setEnabled(false);
		email.setEnabled(false);

		JPanel panel_modif = new JPanel(new GridLayout(7, 1));
		panel_modif.setBorder(BorderFactory.createEmptyBorder(6, 3, 6, 6));

		panel_modif.add(new JLabel("Note"));
		note = new JTextField();
		note.setToolTipText("La note en chiffre.");
		panel_modif.add(note);
		panel_modif.add(new JLabel("Evaluateur"));
		correcteur = new JTextField();
		panel_modif.add(correcteur);
		panel_modif.add(new JLabel("Date"));
		date_correction = new JTextField();
		date_correction.setToolTipText("La date au format yyyy-mm-dd");
		date_correction.setText("yyyy-mm-dd");
		panel_modif.add(date_correction);
		definitive = new JCheckBox();
		definitive.setText("Definitive");
		panel_modif.add(definitive);

		principal.add(panel_modif);

		Note note_saisie = modele.findNote(modele.getEtudiantCourant());
		if (note_saisie != null) {
			note.setText("" + note_saisie.getNote());
			note.requestFocus();
			correcteur.setText(note_saisie.getCorrecteur());
			date_correction
					.setText(note_saisie.getDate_correction());
			definitive.setSelected(note_saisie.estDefinitive());
			if (note_saisie.estDefinitive()) {
				note.setEnabled(false);
			}
			creation = false;
		} else {
			creation = true;
		}

		save.addActionListener(new ControlJButtonSaveNote(this, modele,
				creation));

		this.pack();

	}

	public double getNote() {
		if (!note.getText().equals("")) {
			return Double.parseDouble(note.getText());
		} else {
			return -1;
		}
	}

	public String getDate_correction(){ 
		return this.date_correction.getText();
	}

	public boolean estDefinitive() {
		return definitive.isSelected();
	}

	public String getEmail() {
		return this.modele.getEtudiantCourant();
	}

	public String getCorrecteur() {
		return correcteur.getText();
	}

}
